package com.pospi.pospihold.ui.vip_ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.pospi.pospihold.R;
import com.pospi.pospihold.dto.GoodsDto;
import com.pospi.pospihold.fragment.VIPFavourFragment;
import com.pospi.pospihold.ui.base.BaseActivity;
import com.pospi.pospihold.util.TabEntity;
import com.pospi.pospihold.util.ViewFindUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public class VIPFavourActivity extends BaseActivity {

    private List<String> titles = null;
    private int not_use = 1;
    private int out_of_date = 1;
    private int used = 1;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private ViewPager vp;
    private CommonTabLayout tl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_vipfavour);
        setTitle("会员优惠券");
        ButterKnife.bind(this);


        titles = new ArrayList<>();
        titles.add("未使用(" + not_use + ")");
        titles.add("已过期(" + out_of_date + ")");
        titles.add("已使用(" + used + ")");
        init();
    }

    private void init() {
        String[] mTitles = new String[titles.size()];
        List<GoodsDto> goodsBeens = new ArrayList<>();
        goodsBeens.clear();
        for (int i = 0; i < titles.size(); i++) {
            mTitles[i] = titles.get(i);
            mTabEntities.add(new TabEntity(mTitles[i]));
            mFragments.add(new VIPFavourFragment());
        }
        View decorView = getWindow().getDecorView();
        vp = ViewFindUtils.find(decorView, R.id.vp);

        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(mAdapter);

        tl = ViewFindUtils.find(decorView, R.id.tl);
        tl.setTabSpaceEqual(true);
        tl();
    }

    private void tl() {
        tl.setTabData(mTabEntities);

        tl.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tl.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp.setCurrentItem(1);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
